package dao;

import com.db.DBConn;
import model.Admin;
import model.Teacher;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.Vector;

public class TeacherDao {
    public Collection getDatas() throws SQLException {
        DBConn db = new DBConn();
        ResultSet rs = db.queryData("select * from teacher order by id");
        Collection col = new Vector();
        while (rs.next()) {
            Teacher teacher = new Teacher();
            teacher.setId(rs.getString("id"));
            teacher.setTno(rs.getString("tno"));
            teacher.setTname(rs.getString("tname"));
            teacher.setPassword(rs.getString("password"));
            teacher.setPhone(rs.getString("phone"));
            col.add(teacher);
        }
        return col;
    }

    //提取教师信息by id
    public Teacher getData(String id) throws SQLException {
        DBConn db = new DBConn();
        ResultSet rs = db.queryData("select * from teacher where id="+id);
        if(rs.next()) {
            Teacher teacher = new Teacher();
            teacher.setId(id);
            teacher.setTno(rs.getString("tno"));
            teacher.setTname(rs.getString("tname"));
            teacher.setPassword(rs.getString("password"));
            teacher.setPhone(rs.getString("phone"));
            return teacher;
        }else {
            return null;
        }
    }

    //修改数据
    public void updateData(Teacher teacher) throws SQLException {
        DBConn db = new DBConn();
        //String ano = teacher.getAno();
        db.updateData("update teacher set tno='"+teacher.getTno()+"',tname='"+teacher.getTname()+"',password='"+teacher.getPassword()+"',phone='"+teacher.getPhone()+"' where id="+teacher.getId());
    }

    //新增数据
    public void insertData(Teacher teacher) throws SQLException {
        DBConn db = new DBConn();
        db.updateData("insert into teacher(tno,tname,password,phone) values('"+teacher.getTno()+"','"+teacher.getTname()+"','"+teacher.getPassword()+"','"+teacher.getPhone()+"')");
    }

    //删除数据
    public void deleData(String id) throws SQLException {
        DBConn db = new DBConn();
        db.updateData("delete from teacher where id="+id);

    }
}